import Database from '@tauri-apps/plugin-sql';

class DbService {
    private db!: Database;
    private dbReady: Promise<void>;

    constructor() {
        this.dbReady = this.initDatabase();
    }

    private async initDatabase() {
        try {
            this.db = await Database.load("sqlite:mlp.db");

            await this.db.execute(`CREATE TABLE IF NOT EXISTS t_performer ( key TEXT PRIMARY KEY, value TEXT )`);


        } catch (error) {
            console.error("Error initializing database:", error);
            throw error;
        }
    }

    public async getDbInstance(): Promise<Database> {
        await this.dbReady;
        return this.db;
    }
}

export default new DbService();